const path = require('path')
const fs = require('fs')

const libPackagePath = path.join(__dirname, 'lib/dataview/datav-vue3/package.json')
const libIndexPath = path.join(__dirname, 'lib/dataview/datav-vue3/es/index.mjs')
const libComponentsPath = path.join(__dirname, 'lib/dataview/datav-vue3/es/components')

const modulesPackagePath = path.join(__dirname, 'node_modules/@dataview/datav-vue3/package.json')
const modulesIndexPath = path.join(__dirname, 'node_modules/@dataview/datav-vue3/es/index.mjs')
const modulesComponentsPath = path.join(__dirname, 'node_modules/@dataview/datav-vue3/es/components')

fs.writeFileSync(modulesPackagePath, fs.readFileSync(libPackagePath));
fs.writeFileSync(modulesIndexPath, fs.readFileSync(libIndexPath));

fs.rmdirSync(modulesComponentsPath, { recursive: true, force: true });

const dirs = [{
  absolutePath: libComponentsPath,
  realtivePath: ''
}]

for(let dir of dirs) {
  fs.mkdirSync(path.join(modulesComponentsPath, dir.realtivePath))
  const names = fs.readdirSync(dir.absolutePath)
  for(let name of names) {
    const stat = fs.statSync(path.join(dir.absolutePath, name))
    if(stat.isDirectory()) {
      dirs.push({
        absolutePath: path.join(dir.absolutePath, name),
        realtivePath: path.join(dir.realtivePath, name)
      })
    } else if(stat.isFile()) {
      fs.writeFileSync(path.join(modulesComponentsPath, dir.realtivePath, name), fs.readFileSync(path.join(dir.absolutePath, name)))
    }
  }
}
